/**
*	插件：菜单使用
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.menu = {
	init:function(){
		
	},
	create:function(cans){
		var me = this;
		var defv = js.applyIf(cans,{
			rand:js.getrand(),
			obj:false,
			data:[],
			trigger:'click',
			top:2,
			left:0,
			bgcolor:'',
			background:'white',
			autoremove:true,
			autohide:true,
			bottomheight:0,
			iconswh:16,
			onclick:function(){},
			onresultbody:function(){return ''},
			width:0,
			display:'name', //显示的键
			value:'' //选中的值
		});
		for(var i in defv)this[i]=defv[i];
		var rand  = this.rand;
		var obj	  = this.obj;
		

		//初始化
		this.init	= function(){
			if(!obj)return;
			this.setcontent();
		};
		this.hide	= function(){
			var o = this.mdivobj;
			if(!o)return;
			o.hide();
			if(this.bgcolor!='')obj.css('background','');
			if(this.autoremove)o.remove();
		};
		this.setcontent	= function(){
			$('.rock-menu').remove();
			var json  = this.data;
			if(json.length<=0)return false;
			if(this.bgcolor!='')obj.css('background',this.bgcolor);
			if(this.autoremove)$('#rockmenu_'+rand+'').remove();
			if(get('rockmenu_'+rand+'')){
				this.setweizhi();
				return false;
			}
			var len	= json.length;
			var str	= '<div class="rock-list-group rock-radius rock-menu" style="position:absolute;z-index:9999;background:'+this.background+';user-select:none" id="rockmenu_'+rand+'">';
			str+='<ul>';
			var s	= '',ys='',col,va,hov,d;
			for(var i=0; i<len; i++){
				ys= '';hov=' rock-hover';
				d = json[i];
				va= d[this.display];
				if(i==len-1)ys+='border:none;';
				col	= '';
				if(d.color)ys+='color:'+d.color+';';
				if(va==this.value)col='#e1e1e1';
				if(d.background)col=d.background;
				if(col)ys+='background:'+col+';';
				if(d.disabled){ys+='color:#aaaaaa;';hov= '';}
				s = '<li temp="'+i+'" class="rock-list-item'+hov+'" style="'+ys+'">';
				var s1	= this.onresultbody(d, this, i);
				if(!s1){
					if(d.icons)s+='<img src="'+d.icons+'" width="'+this.iconswh+'" height="'+this.iconswh+'" align="absmiddle">&nbsp;';
					s+=va;
				}else{
					s+=s1;
				}
				s+='</li>';
				str+=s;
			}
			str+='</ul></div>';
			$('body').prepend(str);
			var oac	= $('#rockmenu_'+rand+'');
			oac.find('li').click(function(){me.itemsclick(this);});
			if(this.width!=0){
				oac.css('width',''+this.width+'px');
			};
			js.bodyclick.addbody(rand, 'remove', 'rockmenu_'+rand+''); 
			this.mdivobj = oac;
			this.setweizhi();
			if(this.showXY)this._reshewhere(this.showXY.left,this.showXY.top);
		};
		this.showAt		= function(l, t, w){
			this.setcontent();
			var oac	= this.mdivobj;
			if(!oac)return;
			if(w)this.setWidth(w);
			this._reshewhere(l,t);
		};
		this.offset=function(l,t){
			this._reshewhere(l,t);
		};
		this.getHeight = function(){
			return get('rockmenu_'+rand+'').scrollHeight;
		};
		this._reshewhere=function(l,t){
			var oac	= this.mdivobj;
			var jg	= (l+oac.width()+5 - winWb()),jg1=0;	
			if(jg>0)l=l-jg;
			jg1	= t+get('rockmenu_'+rand+'').scrollHeight+10+this.bottomheight-winHb();
			if(jg1>0)t=t-jg1;
			if(t<5)t=5;
			oac.css({'left':''+l+'px','top':''+t+'px'});
		};
		this.setValue	= function(v){
			this.value	= v;
		};
		this.removeItems		= function(oi){
			$('#rockmenu_'+rand+'').find("li[temp='"+oi+"']").remove();
		};
		this.setWidth	= function(w){
			var oac	= this.mdivobj;
			if(!oac)return;
			oac.css({'width':''+w+'px'});
		};
		this.setweizhi = function(){
			var oac		= this.mdivobj;
			oac.show();
			if(!obj)return;
			var off		= obj.offset(),
				l		= off.left+ this.left,
				t		= off.top+this.top+obj.height();
			this._reshewhere(l,t);
		};
		//项目单击
		this.itemsclick = function(o){
			var oi	= parseInt($(o).attr('temp'));
			var d 	= this.data[oi];
			if(d.disabled)return;
			this.onclick(d,o);
			if(this.autohide)this.hide();
		};
		this.setData	= function(da){
			this.data= da;
			this.autoremove = true;
		};
		this.remove		= function(){
			this.hide();
		}
		
		this.init();
	},
	
	
	
	createbill:function(cans,nda){
		var da = [{name:'详情',ismodal:true,lx:'0'},{name:'详情(新窗口)',ismodal:false,lx:'1'}];
		if(typeof(nda)=='undefined'){
			da.push({name:'<i class="rock-loading"></i>加载...'});
		}else if(typeof(nda)=='object'){
			for(var i=0;i<nda.length;i++)da.push(nda[i]);
		}
		
		new js.menu.create({
			cans:cans,
			data:da,
			obj:cans.obj,
			onclick:function(d){
				js.menu.createclick(d ,this);
			}
		});
		if(typeof(nda)=='undefined'){
			var url = 'api/flow/optmenu?num='+cans.num+'&mid='+cans.mid+'';
			js.ajax.get({
				url:url,
				cnum:cans.cnum,
				funitid:cans.funitid
			},function(ret){
				js.menu.createbill(cans,ret.data);
			});
		}
	},
	createclick:function(d, obj){
		var cans = obj.cans;
		var data = cans.data; //当前行数据
		var lx   = parseFloat(d.lx);
		var list = cans.table.listobj;//列表上对象
		var lbar = list.onmenushow(d, data);
		var showtype = cans.table.showtype;
		var opencan  = {
			num:cans.num,
			cnum:cans.cnum,
			xiangwidth:cans.xiangwidth,
			funitid:cans.funitid,
			mid:cans.mid,
			name:cans.name,
			tableid:cans.table.getid(),
			ismodal:true
		};
		
		if(lx==0){
			xcy.openxiang(opencan);
		}
		if(lx==1){
			opencan.ismodal=false;
			xcy.openxiang(opencan);
		}
		//编辑
		if(lx==2){
			xcy.openinput(opencan);
		}
		if(lx==3){
			opencan.ismodal=false;
			xcy.openinput(opencan);
		}
		if(lx==4){
			js.modal.confirm({
				btntext:'删除',
				msg:'确定要删除此单据吗？',
			}, function(jg){
				if(jg=='yes'){
					cans.table.del({num:cans.num,mid:cans.mid});
				}
			});
		}
		if(lx==5){
			opencan.num = 'remind';
			opencan.mid = '0';
			opencan.name = d.name;
			opencan.funitid = d.funitid;
			var inpa = {
				mknum:cans.num,
				mid:cans.mid,
				mess:d.mess,
				title:cans.name
			};
			if(lbar && lbar.inputdata)for(var i in lbar.inputdata)inpa[i] = lbar.inputdata[i];
			xcy.openinput(opencan,inpa);
		}
		if(lx==6){
			var sid = 'viese'+js.getrand()+'';
			var tan = js.modal.main({
				title:d.name,
				width:750,height:300,bbar:'none',
				html:'<div id="'+sid+'"><div style="padding:50px" align="center">'+js.show.ling(50)+'</div></div>'
			});
			var we = (showtype=='1') ? 'we':'';
			xcy.addcont('flow/list'+we+'',sid, {
				num:'remind',cnum:opencan.cnum,otype:'company',funitid:d.funitid,pnum:'all',gtype:'showmknum',
				sqlwhere:"`mknum`='"+cans.num+"' AND `mid`="+cans.mid+"",height:370
			},function(){
				js.modal.resize(tan.action);
			});
		}
		if(lx==99){
			var type = d.type,str1='';
			opencan.table = cans.table;
			opencan.list  = list;
			if(lbar)str1 = lbar;
			if(type==1 || type==7){
				var str='<div>请输入的“'+d.name+'”说明</div>',pl='说明选填';
				if(d.issm==1)pl='说明必填';
				str+='<textarea class="textarea" name="sm" placeholder="'+pl+'" style="height:60px" id="flowmenu_explain"></textarea>';
				if(d.isup>0){
					str+='	<div id="flowmenu_fileview"></div>';
					str+='	<div><input name="fileid" id="flowmenu_fileview_input" type="hidden"><button type="button" onclick="js.menu.uploadfile(\''+opencan.cnum+'\',\''+d.uptype+'\')" class="webbtn webbtn-mini webbtn-default">上传文件...</button></div>';
				}
				js.modal.main({
					action:'flowmenu',
					width:400,height:300,
					title:d.name,
					html:'<div style="margin:15px"><form name="flowmenuform">'+str1+str+'</form></div>',
					btn:[{text:'确定'}]
				});
				$('#flowmenu_btn0').click(function(){
					js.menu.submitmenu(d,opencan);
				});
			}
			if(type==0){
				js.menu.submitmenu(d,opencan);
			}
		}
	},
	uploadfile:function(cnum,ulx){
		importjs('rockfile','uploafile',{
			name:'上传文件',
			uptype:ulx,
			inputobj:get('flowmenu_fileview_input'),
			cnum:cnum,
			view:'flowmenu_fileview'
		});
	},
	submitmenu:function(d,cans){
		var da = js.getformdata('flowmenuform');
		if(d.issm==1 && !da.sm){
			js.show.error('请输入说明');
			return;
		}
		if(d.isup==1 && !da.fileid){
			js.show.error('必须上传相关文件');
			return;
		}
		var lbar = cans.list.onmenusavebefore(d, da);
		if(lbar && typeof(lbar)=='string'){
			js.show.error(lbar);
			return;
		}
		js.tanclose('flowmenu');
		var url = 'api/flow/optmenurun?num='+cans.num+'&mid='+cans.mid+'&lx='+d.lx+'';
		if(d.menuid)url+='&menuid='+d.menuid+'';
		js.ajax.post({
			url:url,
			cnum:cans.cnum,
			funitid:cans.funitid,
			data:da,
			loading:'提交中...',
			loadsuccess:'提交成功'
		},function(){
			cans.table.reload();
		});
	}
}